package ui.task;

import cerebrum.cache.MacDateTaskStatusCache;
import cerebrum.common.LunarUtil;
import javafx.application.Platform;
import ui.panel.setup.WallpaperTypeChoiceStage;

import java.time.LocalDateTime;
import java.util.TimerTask;

/**
 * mac时钟task
 */
public class MacTimeTask extends TimerTask {
    @Override
    public void run() {
        if (!MacDateTaskStatusCache.getStatus()) {
            cancel();// 停止定时器
        }
        // 更新UI显示
        Platform.runLater(() ->
                 WallpaperTypeChoiceStage.getInstance().setDataClock(dataCheck())
        );
    }

    private String dataCheck() {
        LocalDateTime time = LocalDateTime.now();
        int hour = time.getHour();
        int minute = time.getMinute();
        if (hour >12) {
            hour = hour-12;
        }
        if (hour == 0) {
            hour = 12;
        }
        String hourStr = hour < 10 ? ("0" + hour) : String.valueOf(hour);
        String minuteStr = minute < 10 ? ("0" + minute) : String.valueOf(minute);

        String str ="";
        LocalDateTime time24 = LocalDateTime.now();
        int hourTime = time.getHour();
        if (hourTime<5){
            str = "凌晨";
        }else if (hourTime < 8){
            str = "清晨";
        }else if (hourTime < 12){
            str = "上午";
        }else if (hourTime < 14){
            str = "中午";
        }else if (hourTime < 18){
            str = "下午";
        }else if (hourTime < 22){
            str = "晚上";
        }else if (hourTime >21 ){
            str = "深夜";
        }
        String weekOfDate = LunarUtil.getWeek(time24.getYear(), time24.getMonth().getValue(), time24.getDayOfMonth());
        return weekOfDate+" " + str+" "+hourStr + " : " + minuteStr;
    }
}
